package com.ruoyi.easyfame.aspact;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.easyfame.anno.LogDB;
import com.ruoyi.easyfame.domain.FinanceLog;
import com.ruoyi.easyfame.mapper.FinanceLogMapper;
import org.aopalliance.intercept.Joinpoint;
import org.apache.ibatis.binding.MapperMethod;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Component
public class LogDBAspact {
    @Autowired
    private FinanceLogMapper fingerLogMapper;


    @Pointcut("execution(* com.ruoyi.easyfame.mapper.*.*(..)) && @annotation(com.ruoyi.easyfame.anno.LogDB)")
    public void logPointCut() {
    }

    @AfterReturning(pointcut = "logPointCut()", returning = "result")
    public void logDB(JoinPoint joinPoint, Object result) {
        FinanceLog fingerLog = new FinanceLog();
        fingerLog.setOperationTime(LocalDateTime.now());
        fingerLog.setOperatorId(SecurityUtils.getLoginUser().getUser().getUserId());
        fingerLog.setOperatorName(SecurityUtils.getLoginUser().getUser().getUserName());
        fingerLog.setOperationDept(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        fingerLog.setMethodParams(Arrays.toString(joinPoint.getArgs()));

        // 设置方法返回值
        if (result != null) {
            fingerLog.setReturnValue(result.toString());
        } else {
            fingerLog.setReturnValue("void");
        }
        System.out.println(fingerLog);

        fingerLogMapper.insert(fingerLog);
    }
}
